import {defineStore} from "pinia";
import {ElMessage} from "element-plus";
import {addMembers, delMembers, findAllMembers, getMembers, isApply, updateMembers} from "@/api/members";

export const MembersStore = defineStore('members', {
    state:()=>{
        return {
            // 列表
            membersList: [],
            // 详细信息
            membersInfo: {}
        }
    },
    actions:{
        isApply(){
            return new Promise((resolve, reject)=>{
                isApply().then(res => {
                    resolve(res.result)
                }).catch(err => {
                    reject()
                })
            })
        },
        addMembers(){
            return new Promise((resolve, reject)=>{
                addMembers(this.membersInfo).then(res => {
                    resolve(res.result)
                    this.membersInfo = {}
                }).catch(err => {
                    reject()
                })
            })
        },
        async getMembersList(query) {
            return new Promise((resolve, reject)=>{
                findAllMembers(query).then(res => {
                    this.membersList = res.result.list
                    resolve(res.result)
                }).catch(err => {
                    reject()
                })
            })
        },
        async getMembersInfo(id) {
            const {result} = await getMembers(id)
            this.membersInfo = result
            this.membersInfo.members_price = parseFloat(this.membersInfo.members_price)
        },
        updateMembers(id,data) {
            return new Promise(async (resolve, reject) => {
                updateMembers(id,data).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        async deleteMembers(id) {
            const {code} = await delMembers(id)
        },
    }
})